/*
  Copyright (c) 2011, Mikheev Rostislav
  Copyright (c) 2011, FreshFarsh team

  Steel Engine source code is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/**
  @author Mikheev Rostislav <hacenator@gmail.com>
  @date 13.03.2011
  @brief OpenGL Camera class source file
*/

#include "camera.h"

namespace _STEEL_NAMESPACE {

Camera::Camera()
  : _initialized(false),
  _modified(true)
{

}

Camera::~Camera()
{
    //dtor
}

void Camera::Init(int width, int height) {
  _viewport_width = width;
  _viewport_height = height;

  Reset();

  glGetFloatv(GL_PROJECTION_MATRIX, &_matrix.array[0]);

  _initialized = true;
  _modified = false;
}

void Camera::Apply() {
  if (_initialized) {
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(&_matrix.array[0]);

    _modified = false;
  }
}

void Camera::Reset() {
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, _viewport_width, _viewport_height, 0, -1, 1); // Ortho2D
}

void Camera::Translate(float dx, float dy) {
  _matrix.element[3][0] += dx;
  _matrix.element[3][1] += dy;

  _modified = true;
}

void Camera::MoveTo(float x, float y) {
  _matrix.element[3][0] = x;
  _matrix.element[3][1] = y;

  _modified = true;
}

float Camera::GetOffsetX() {
  return _matrix.element[3][0];
}

float Camera::GetOffsetY() {
  return _matrix.element[3][1];
}

float Camera::GetOffsetZ() {
  return _matrix.element[3][2];
}

}
